[% setvar title Dominant Value Expressions %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Dominant Value Expressions</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Glenn Linderman &lt;<a href='mailto:glenn@linderman.com'>glenn@linderman.com</a>&gt;
  Date: 29 Sep 2000
  Last Modified: 30 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 356
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>An aid to determining if an input  value has an impact on the result of
an expression whole program.  Can also be used for Perl poetry.</p>
<a name='CHANGES'></a><h1>CHANGES</h1>
<p>Version 2 adds output function, examples, and freezes.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>This  is an  optional  feature; to  turn  it on,  &quot;use domination;&quot;  is
suggested.</p>
<p>When use domination  is in scope, two new  functions are available, and
new rules for expression evaluation obtain.  Each of these is described
in a subsection below.</p>
<a name='Domination pragma'></a><h2>Domination pragma</h2>
<p>The &quot;use domination 'output-function-name';&quot; pragma enables the rest of
the  functionality.  It  should be  scoped, affecting  the  current and
nested blocks.  domination;&quot;, but allows user specification of a method
to  use  to  convert  dominant  values  to  output  strings.   This  is
implicitly  called only  when an  output  stream is  passed a  dominant
value.   If   an  output-function-name   is  not  supplied   with  &quot;use
domination;&quot;, the following function is implied:</p>
<pre>  sub output-dominant-value {
    return sprintf &quot;DOMINANT(%g)&quot;, dominant_weight($_[0]);
  }</pre>
<p>The  &quot;no  domination;&quot;  pragma  would  turn  off  the  effect  of  &quot;use
domination;&quot; for the current and nested blocks.  If a dominant value is
encountered  while &quot;no  domination;&quot; is  in  effect, it  is treated  as
&quot;undef&quot; by all scalar operators.</p>
<a name='Dominant operation'></a><h2>Dominant operation</h2>
<p>The dominant operation takes a  scalar argument, which is considered to
be a weight parameter.  The scalar argument is converted to numeric, if
possible,  with the  resultant  positive weight  parameter producing  a
dominant value with  that given weight.  A scalar  argument that cannot
be automatically converted  to numeric, or that produces  a negative or
zero numeric value produces undef as a result.</p>
<pre>   dominant 47    # produces a dominant value of weight 47
   dominant 0     # produces undef
   dominant -47   # produces undef
   dominant &quot;ab&quot;  # produces undef
   dominant &quot;14&quot;  # produces a dominant value of weight 14
   dominant undef # produces undef (and a warning if &quot;use warnings&quot;)</pre>
<a name='Dominant_weight operation'></a><h2>Dominant_weight operation</h2>
<p>The dominant_weight  operation takes a scalar argument.   If the scalar
argument  is a  dominant value,  it returns  its weight  as  a positive
number.  If the scalar argument is  not a dominant value, the return of
the dominant_weight operation is zero.</p>
<pre>   dominant_weight dominant 47   # produces 47
   dominant_weight dominant 0    # produces 0
   dominant_weight dominant -47  # produces 0
   dominant_weight dominant &quot;ab&quot; # produces 0
   dominant_weight dominant &quot;14&quot; # produces 14
   dominant_weight 47            # produces 0
   dominant_weight 0           # produces 0
   dominant_weight &quot;ab&quot;      # produces 0
   dominant_weight &quot;14&quot;    # produces 0
   dominant_weight undef # produces 0 (and a warning if &quot;use warnings&quot;)</pre>
<a name='Expressions involving dominant values'></a><h2>Expressions involving dominant values</h2>
<p>All scalar operations are affected  by the presence of dominant values.
If a scalar operation other than the dominant_weight operation involves
one  or  more dominant  values,  the result  of  the  operation is  the
heaviest (by weight) dominant value among the operands.  If no dominant
values are  supplied to the operation,  the result of  the operation is
the  same as  it would  be  according to  the usual  definition of  the
operation.</p>
<pre>   use domination;
   $w = dominant 3;
   $x = dominant 47;
   $y = 33;
   $z = &quot;abc&quot;;

   $x + $y                # produces dominant 47
   $w . $z                # produces dominant 3
   $w - $x                # produces dominant 47
   &quot;$z $x&quot;                # produces dominant 47
   $z =~ m/$w/;           # produces dominant 3
   $x =~ m/$z/;           # produces dominant 47
   defined $w             # produces dominant 3
   $x == $x               # produces dominant 47
   $w eq $w               # produces dominant 3
   $w &gt; 17                # produces dominant 3
   $x &gt; 17                # produces dominant 47
   print &quot;Show me: $w\n&quot;  # same result as: print &quot;DOMINANT(3)&quot;

   if ( $w )                       # considered false
   if ( dominant_weight $w &gt; 17 )  # false
   if ( dominant_weight $x &gt; 17 )  # true</pre>
<a name='COMPATIBILITY'></a><h1>COMPATIBILITY</h1>
<p>New functionality, no compatibility issues.  The new functionality only
obtains if the &quot;use domination;&quot; pragma is in effect.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>The impact  of dominant value  expressions is pervasive,  affecting all
builtin scalar operators  in a minor way.  Any  operators doing item by
item operations  on each scalar  of a list  or hash would  be similarly
affected (various RFCs exist for extending scalar operations to work on
lists of values in &quot;corresponding item&quot; fashion).</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 263: Add null() keyword and fundamental data type</p>
</div>
